
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>角色管理</title>
    <#include "../common/link.ftl">

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <c:set var="currentMenu" value="role"/>
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>角色编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/role/saveOrUpdate.do" method="post" id="editForm">

                    <input type="hidden" value="${(role.id)!}" name="id">
                    <div class="form-group"  style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">角色名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(role.name)!}" placeholder="请输入角色名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="sn" class="col-sm-2 control-label">角色编号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sn" name="sn" value="${(role.sn)!}" placeholder="请输入角色编号">
                        </div>
                    </div>
                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配权限：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allPermissions" size="15">
                                    <!--  回显权限数据 -->
                                    <#list permissions as p>
                                        <option value="${p.id}">${p.name}</option>
                                    </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary" style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>
                            <div class="col-sm-2">
                                <select multiple class="form-control selfPermissions" size="15"  name="ids">
                                    <!-- 获取右边框角色的信息 -->
                                    <#list (role.permissions)! as r>
                                        <option value="${r.id}">${r.name}</option>
                                    </#list>
                                </select>
                            </div>
                          <script>
                              // 移动单个
                            function moveSelected(src,targer) {
                                $("."+targer).append($("."+ src +"> option:selected"))
                            }
                              // 移动所有
                            function moveAll(src,targer) {
                                $("."+targer).append($("."+ src +"> option"))
                            }
                            </script>

                            <script>
                                $(function () {
                                    // 需求:回显时的角色去重
                                    // 1.获取右边以及拥有的角色dom对象数组,遍历去获取到每个option的vlaue
                                    // 存放到新的数组中 ids=[ ] , each(参数1,参数2,参数3(默认值))遍历
                                    // 参数2 : DOM元素 <value = 1,经理>
                                    var ids = []; // 只要存在改数组中的id,代表当前员工已经拥有的角色id
                                    $(".selfRoles > option").each(function (i,ele) {
                                        ids.push($(ele).val());
                                    })
                                    // 2.遍历左边的每一个option,获取到对应的value值,判断ids数组中是否存在对应的值,
                                    // 有就删除自己
                                    $(".allRoles > option").each(function (i, ele) {
                                        var id = $(ele).val();
                                        // 通过Jquery对象调用inArray()方法
                                        //  inArray(id,数组) 去重方法,返回的是索引值,当索引大于-1说明有重复数据
                                        if ($.inArray(id,ids)> -1){
                                            $(ele).remove(); //删除所在option标签
                                        }
                                    })
                                })
                            </script>

                            <script>
                                $(function () {
                                    $("#btn_submit").click(function () {
                                        $(".selfPermissions > option").prop('selected',true)
                                        // 提交表单
                                        $("#editForm").submit()
                                    })
                                })
                            </script>

                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="btn_submit" type="button" class="btn btn-primary">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>
</body>
</html>
